#pragma once
#include <iostream>

namespace mystl {

    static const int init_cap = 3;

    template<class T>
    class vector 
    {
    public:

        class Iterator 
        {
        public:
            Iterator(T* ptr) 
                :_ptr(ptr)
            {}
            Iterator& operator++()
            {
                ++_ptr;
                return *this;
            }
            bool operator!=(const Iterator& other)
            {
                return _ptr != other._ptr;
            }
            T operator*()
            {
                return *_ptr;
            }
        private:
            T* _ptr = nullptr;
        };
        vector()
            :_data((T*)malloc(sizeof(T) * init_cap)),
            _start(_data),
            _size(_data),
            _cap(_data + init_cap)
        {}
        Iterator begin() const
        {
            return Iterator(_start);
        }
        Iterator end() const
        {
            return Iterator(_size);
        }
        void push_back(const T& val) 
        {
            uint64_t size = _size - _start;
            uint64_t cap = _cap - _start;
            if (size == cap)
            {
                //扩容
                uint64_t newcap = cap * 2;
                _data = (T*)realloc(_data,(sizeof(T) * newcap));
                _cap = _start + newcap;
            }
            _data[size] = val;
            ++_size;
        }
        void pop_back()
        {
            if(_size - _start > 0) --_size;
        }
    private:
        T* _data = nullptr;
        T* _start = nullptr;
        T* _size = nullptr;
        T* _cap = nullptr;
    };
}